package com.yc.sole.erp.biz;

import cn.hutool.core.bean.BeanUtil;
import com.yc.sole.erp.dto.ShiftDto;
import com.yc.sole.erp.repository.ShiftRepository;
import com.yc.sole.erp.request.ShiftQueryReq;
import com.yc.sole.erp.request.ShiftReq;
import com.yc.sole.erp.response.ShiftResp;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-01-25
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ShiftBiz {

    private final ShiftRepository shiftRepository;

    /**
     * 新增
     *
     * @param shiftReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveShift(ShiftReq shiftReq) {
        ShiftDto dto = new ShiftDto();
        BeanUtil.copyProperties(shiftReq, dto);
        shiftRepository.saveShift(dto);
    }

    /**
     * 修改
     *
     * @param shiftReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateShiftById(ShiftReq shiftReq) {
        ShiftDto dto = new ShiftDto();
        BeanUtil.copyProperties(shiftReq, dto);
        shiftRepository.updateShiftById(dto);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteShiftByIds(Collection<Long> ids) {
        shiftRepository.deleteShiftByIds(ids);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public ShiftResp selectShiftById(Long id) {
        return BeanUtil.copyProperties(shiftRepository.selectShiftById(id), ShiftResp.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param shiftQueryReq
     * @return
     */
    public Paging<ShiftResp> selectShiftPage(Paging<ShiftQueryReq> page, ShiftQueryReq shiftQueryReq) {
        Paging<ShiftDto> paging = shiftRepository.selectShiftPage(page, shiftQueryReq);
        return PageUtil.pageCopy(paging, ShiftResp.class);
    }

    /**
     * 根据名称查询
     *
     * @return
     */
    public List<ShiftResp> selectAllShift() {
        return BeanUtil.copyToList(shiftRepository.selectAllShift(), ShiftResp.class);
    }
}
